package com.lgq.mydatabackend.controller;

import com.lgq.mydatabackend.pojo.Result;
import com.lgq.mydatabackend.pojo.User;
import com.lgq.mydatabackend.service.UserService;
import com.lgq.mydatabackend.utils.JwtUtil;
import com.lgq.mydatabackend.utils.Md5Util;
import com.lgq.mydatabackend.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        // 判断用户名是否存在
        if (userService.findByUsername(username) != null) {
            return Result.error("用户名已存在");
        }
        // 注册
        userService.register(username, password);
        return Result.success();
    }

    @PostMapping("/login")
    public Result login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        // 用户是否存在
        User loginUser = userService.findByUsername(username);
        if (loginUser == null) {
            return Result.error("该用户不存在");
        }
        // 判断密码是否一致
        if (!Md5Util.getMD5String(password).equals(loginUser.getPassword())) {
            return Result.error("密码错误");
        }

        // 登陆成功
        Map<String, Object> claima = new HashMap<>();
        claima.put("id", loginUser.getId());
        claima.put("username", loginUser.getUsername());
        String token = JwtUtil.genToken(claima);

        return Result.success(token);
    }

    @GetMapping("/userInfo")
    public Result<User> getUserInfo() {
        // 从ThreadLocal里拿用户名
        Map<String, Object> map = ThreadLocalUtil.get();
        User user = userService.findByUsername((String) map.get("username"));
        return Result.success(user);
    }

    @PutMapping("/update")
    public Result update(@RequestBody @Validated User user) {
        userService.update(user);
        return Result.success();
    }

    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam @URL String userPic) {
        userService.updateAvatar(userPic);
        return Result.success();
    }

    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String, String> params) {
        // 参数校验
        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");

        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return Result.error("缺少必要的参数");
        }

        // 两次输入是否一致
        if (!newPwd.equals(rePwd)) {
            return Result.error("两次输入密码不一致");
        }

        // 判断新旧密码是否相同
        if (oldPwd.equals(newPwd)) {
            return Result.error("修改后的密码不能与原密码相同");
        }

        String md5String = Md5Util.getMD5String(oldPwd);
        Map<String, Object> map = ThreadLocalUtil.get();
        String userName = (String) map.get("username");
        User user = userService.findByUsername(userName);

        // 原密码是否正确
        if (!user.getPassword().equals(md5String)) {
            return Result.error("原密码错误");
        }

        // 调用service完成修改
        userService.updatePwd(Md5Util.getMD5String(newPwd), user.getId());
        return Result.success();
    }
}
